package com.jiuzhe.commercial.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.base.BaseFragment;
import com.jiuzhe.commercial.bean.AccountInfoResult;
import com.jiuzhe.commercial.bean.CalendarBean;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.http.CallBack;
import com.jiuzhe.commercial.http.HttpClient2;
import com.jiuzhe.commercial.ui.my.AccountListActivity;
import com.jiuzhe.commercial.ui.my.DetailListActivity;
import com.jiuzhe.commercial.ui.my.IdeaActivity;
import com.jiuzhe.commercial.ui.my.UserActivity;
import com.jiuzhe.commercial.ui.my.WithdrawActivity;
import com.jiuzhe.commercial.utils.RxBus;
import com.jiuzhe.commercial.utils.SPUtil;
import com.jiuzhe.commercial.utils.UIUtils;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscription;
import rx.functions.Action1;


/**
 * Created by 12078 on 2018/4/19.
 */

public class MyFragment extends BaseFragment {

    @BindView(R.id.ib_location)
    ImageView ibLocation;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.head_title)
    TextView headTitle;
    @BindView(R.id.ib_add)
    ImageView ibAdd;
    @BindView(R.id.ib_switcher)
    ImageView ibSwitcher;
    @BindView(R.id.tv_query)
    TextView tvQuery;
    @BindView(R.id.civ_head)
    ImageView civHead;
    @BindView(R.id.tv_userNickname)
    TextView tvUserNickname;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.ib_forward)
    ImageButton ibForward;
    @BindView(R.id.ll_user)
    LinearLayout llUser;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_detail)
    TextView tvDetail;
    @BindView(R.id.tv_withdraw)
    TextView tvWithdraw;
    @BindView(R.id.tv_idea)
    TextView tvIdea;
    @BindView(R.id.tv_faccount) //
    TextView tvFaccount;
    Subscription mSubscription;


    @Override
    protected int getLayoutResource() {
        return R.layout.my_fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        headTitle.setText("我的");
        ibLocation.setVisibility(View.GONE);
        ibAdd.setVisibility(View.GONE);
        ibSwitcher.setVisibility(View.GONE);
        tvUserNickname.setText(SPUtil.getString(getActivity(),Constants.REALNAME));
        tvPhone.setText(SPUtil.getString(getActivity(),Constants.PHONE));
        mSubscription = RxBus.getInstance().toObserverable(CalendarBean.class)
                .subscribe(new Action1<CalendarBean>() {
                    @Override
                    public void call(CalendarBean bean) {
                        initData();
                    }
                });
    }



    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        String id = SPUtil.getString(getContext(), Constants.ID);
        String mr_id = SPUtil.getString(getContext(), Constants.MERCHANTID);
        String token = SPUtil.getString(getContext(), Constants.TOKEN);
        String autoSignStr = Constants.FACCOUNT.substring(Constants.FACCOUNT.lastIndexOf("0/")+1) + "/" + mr_id + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);

        String url = Constants.FACCOUNT  + "/" + mr_id + "?token=" + id + "&&sign=" + signStr;

          HttpClient2.get(this, url , map, new CallBack<List<String>>() {
            @Override
            public void onSuccess(List<String> result){
               if(result.get(0).equals("31")) {
                    AccountInfoResult accountInfoResult = JSON.parseObject(result.get(2), AccountInfoResult.class);
                    java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
                    tvFaccount.setText("¥" + df.format(accountInfoResult.getTotal_balance() / 100.00) + "");
                    Constants.TOTALMONEY = df.format(accountInfoResult.getTotal_balance()/100.00)+"";
                }else if (result.get(0).equals("16")){
                    UIUtils.showToastSafe("账户不存在");
                }
           }
      });
    }


    @OnClick({R.id.tv_withdraw,R.id.tv_idea,R.id.ll_user,R.id.ll_account,R.id.tv_detail})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_withdraw:
                UIUtils.startActivity(WithdrawActivity.class);
                break;
            case R.id.tv_idea:
                UIUtils.startActivity(IdeaActivity.class);
                break;
            case R.id.ll_user:
                UIUtils.startActivity(UserActivity.class);
                break;
            case R.id.ll_account:
                UIUtils.startActivity(AccountListActivity.class);
                break;
            case R.id.tv_detail:
                UIUtils.startActivity(DetailListActivity.class);
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mSubscription != null ) {
            if(!mSubscription.isUnsubscribed()){
                mSubscription.unsubscribe();
            }
        }
    }
}



